package com.glory.good_dark.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.good_dark.R;
import com.glory.good_dark.application.App;
import com.glory.good_dark.entity.EqpInfoEntity;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * FileName: DefectListAdapter
 * Author: XJH
 * Date: 2020/3/31 18:09
 * Description: 不良数量适配器
 * History:
 */
public class DefectListAdapter extends BaseQuickAdapter<EqpInfoEntity.RejectQt, BaseViewHolder> {

    private List<String> defectList;
    private FreeUI_GeneralPop defectPop;
    private TextView outputNum;
    private int totalNum = 0;

    public DefectListAdapter(@Nullable List<EqpInfoEntity.RejectQt> data,TextView outputNum) {
        super(R.layout.adapter_defect,data);
        defectList = new ArrayList<>();
        this.outputNum = outputNum;
    }

    public void setDefectList(List<String> defectList){
        this.defectList = defectList;
    }

    public void setTotalNum(int totalNum){
        this.totalNum = totalNum;
    }

    @Override
    protected void convert(BaseViewHolder helper, EqpInfoEntity.RejectQt item) {
        helper.setText(R.id.tvDefect,item.getREJECTS_CODE());
        helper.setText(R.id.etNum1,String.valueOf(item.getQT_1()));
        helper.setText(R.id.etNum2,String.valueOf(item.getQT_2()));

        EditText etNum1 = helper.getView(R.id.etNum1);
        EditText etNum2 = helper.getView(R.id.etNum2);
        FrameLayout flDefect = helper.getView(R.id.flDefectList);
        TextView tvDelete = helper.getView(R.id.tvDelete);



        flDefect.setOnClickListener(v -> {
            if (defectList.size() <= 0) {
                FreeApi_ToastUtils.showFreeToast("未获取到不良代码列表",mContext,false,Toast.LENGTH_LONG);
                return;
            }
            defectPop = FreeApi_PopUtils.getNormalPop(App.app, flDefect.getWidth(), defectList, helper.getView(R.id.ivDefectListArrow), (code, i) -> {
                item.setREJECTS_CODE(code.split("/")[0]);
                helper.setText(R.id.tvDefect,code.split("/")[0]);
            });
            FreeApi_PopUtils.setArrowShow(helper.getView(R.id.ivDefectListArrow));
            defectPop.showAsDropDown(flDefect, 0, 0);

        });

        tvDelete.setOnClickListener(v -> {
            remove(helper.getAdapterPosition());
            notifyDataSetChanged();
//            int outputNum = Calculation();
//            this.outputNum.setText(outputNum+"");
        });

        setEditListener(etNum1,item);

//        etNum1.setOnFocusChangeListener((v, hasFocus) -> {
//            if(hasFocus) {
//            }else{
//                try {
//                    if(etNum1.getText().toString().equals(""))
//                    {
//                        etNum1.setText(0+"");
//                    }
//                    int outputNum = Calculation();
//                    if(outputNum<0) {
//                        FreeApi_ToastUtils.showFreeToast("产出数小于0，请重新输入", App.app, false, Toast.LENGTH_LONG);
//                        etNum1.setText(0+"");
//                    }else{
//                        this.outputNum.setText(outputNum+"");
//                    }
//
//                } catch (NumberFormatException e) {
//                    e.printStackTrace();
//                    FreeApi_ToastUtils.showFreeToast("计算异常，请输入合法数字", App.app, false, Toast.LENGTH_LONG);
//                }
//            }
//        });

        etNum2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    item.setQT_2(Integer.parseInt(s.toString()));
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                    item.setQT_2(0);
                }
            }
        });

    }

    private void setEditListener(EditText editText, EqpInfoEntity.RejectQt item) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    item.setQT_1(Integer.parseInt(s.toString()));
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }
        };

        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }

    private int Calculation(){
        int sun = 0;
        List<EqpInfoEntity.RejectQt> data = getData();
        if (data.size() <= 0){
            return 0;
        }
        for (EqpInfoEntity.RejectQt bean : data){
            sun = sun + bean.getQT_1();
        }

        if (sun > totalNum){
            sun = -1;
        }
        return sun;
    }
}
